package com.lvyq.myplayer.adapter.singer

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.lvyq.myplayer.R
import com.lvyq.myplayer.parcelable.SingerInfoData
import com.squareup.picasso.Picasso
import jp.wasabeef.picasso.transformations.CropCircleTransformation

/**
 * 文件描述：SingerAdapter
 * 作者：lvyq
 * 创建时间：2025-04-22
 * 修改时间：2025-04-22
 */
class SingerAdapter(
     var list: ArrayList<SingerInfoData>,
    private val itemClickListener: OnItemClickListener
) : RecyclerView.Adapter<SingerAdapter.ViewHolder>(){

    interface OnItemClickListener {
        fun onItemClick(position: Int, data: SingerInfoData)
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val view = LayoutInflater.from(parent.context).inflate(R.layout.item_singer_grid, parent, false)
        return ViewHolder(view)
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        holder.bind(list[position])
        holder.itemView.setOnClickListener {
            itemClickListener.onItemClick(position, list[position])
        }
    }

    override fun getItemCount(): Int {
        return list.size
    }

    fun updateData(newData: ArrayList<SingerInfoData>) {
        list.clear()
        list.addAll(newData)
        notifyDataSetChanged()
    }

    inner class ViewHolder(view: View) : RecyclerView.ViewHolder(view) {
        val img: ImageView = view.findViewById(R.id.singerImg)
        val singer: TextView = view.findViewById(R.id.singerName)

        fun bind(data: SingerInfoData) {
            Picasso.with(itemView.context)
                .load(data.img)
                .transform(CropCircleTransformation())
                .into(img)
            singer.text = data.name
        }
    }


}